﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class LotAttributeCode : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F2271;
        protected HtmlInputHidden hdnFrom;
        protected Label lblDescription;
        protected Label lblLotAttributeCode;
        protected Label lblLotAttributeCodeGroupID;
        protected Label lblLotAttributeCodeID;
        protected Label lblMsg;
        protected Label lblSeqNo;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescription;
        protected TextBox txtLotAttributeCodeGroupID;
        protected TextBox txtLotAttributeCodeID;
        protected TextBox txtSeqNo;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtLotAttributeCodeGroupID.Text = "";
            this.txtLotAttributeCodeID.Text = "";
            this.txtLotAttributeCodeGroupID.Enabled = true;
            this.txtLotAttributeCodeID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtLotAttributeCodeGroupID.Text.Trim()) && !string.IsNullOrEmpty(this.txtLotAttributeCodeID.Text.Trim()))
            {
                try
                {
                    List<string> groupID = new List<string>();
                    List<string> codeID = new List<string> {
                        this.txtLotAttributeCodeGroupID.Text.Trim(),
                        this.txtLotAttributeCodeID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(groupID, codeID, new List<string>(), "LOT_ATTRIBUTE_CODE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("LotAttributeCodeQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("LotAttributeCodeList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLotAttributeCodeGroupID.Text.Trim()) || string.IsNullOrEmpty(this.txtLotAttributeCodeID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2610_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateLotAttributeCodeInfo();
            }
            else
            {
                this.InsertLotAttributeCodeInfo();
            }
        }

        private void InsertLotAttributeCodeInfo()
        {
            try
            {
                LotAttributeCodeInfo lotAttributeCode = new LotAttributeCodeInfo {
                    GroupID = this.txtLotAttributeCodeGroupID.Text.Trim().ToUpper(),
                    CodeID = this.txtLotAttributeCodeID.Text.Trim().ToUpper(),
                    SeqNo = string.IsNullOrEmpty(this.txtSeqNo.Text.Trim()) ? 0 : int.Parse(this.txtSeqNo.Text.Trim()),
                    Descr = this.txtDescription.Text.Trim(),
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(lotAttributeCode, "LOT_ATTRIBUTE_CODE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = lotAttributeCode;
                this.txtLotAttributeCodeGroupID.Enabled = false;
                this.txtLotAttributeCodeID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C004") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!(string.IsNullOrEmpty(base.Request.QueryString["ID"]) || string.IsNullOrEmpty(base.Request.QueryString["ID2"])))
            {
                this.SetLotAttributeCodeInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLotAttributeCodeInfo2Form()
        {
            try
            {
                string groupID = base.Request.QueryString["ID"].ToString();
                string codeID = base.Request.QueryString["ID2"].ToString();
                LotAttributeCodeInfo lotAttributeCode = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(groupID, codeID, "", "LOT_ATTRIBUTE_CODE") as LotAttributeCodeInfo;
                base.DataEntityViewState = lotAttributeCode;
                if (lotAttributeCode != null)
                {
                    this.txtLotAttributeCodeGroupID.Text = lotAttributeCode.GroupID;
                    this.txtLotAttributeCodeID.Text = lotAttributeCode.CodeID;
                    this.txtSeqNo.Text = lotAttributeCode.SeqNo.ToString();
                    this.txtDescription.Text = lotAttributeCode.Descr;
                    this.chkIsActive.Checked = lotAttributeCode.IsActive == "Y";
                    this.txtLotAttributeCodeGroupID.Enabled = false;
                    this.txtLotAttributeCodeID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateLotAttributeCodeInfo()
        {
            try
            {
                LotAttributeCodeInfo lotAttributeCode = base.DataEntityViewState as LotAttributeCodeInfo;
                lotAttributeCode.GroupID = this.txtLotAttributeCodeGroupID.Text.Trim().ToUpper();
                lotAttributeCode.CodeID = this.txtLotAttributeCodeID.Text.Trim().ToUpper();
                lotAttributeCode.SeqNo = string.IsNullOrEmpty(this.txtSeqNo.Text.Trim()) ? 0 : int.Parse(this.txtSeqNo.Text.Trim());
                lotAttributeCode.Descr = this.txtDescription.Text.Trim();
                lotAttributeCode.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                lotAttributeCode.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(lotAttributeCode, "LOT_ATTRIBUTE_CODE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

